define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wl_dingdan/index' + location.search,
                    add_url: 'wl_dingdan/add',
                    edit_url: 'wl_dingdan/edit',
                    del_url: 'wl_dingdan/del',
                    multi_url: 'wl_dingdan/multi',
                    import_url: 'wl_dingdan/import',
                    table: 'wl_dingdan',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('运单列表'),
                                    title: __('运单列表'),
                                    classname: 'btn btn-xs btn-primary btn-dialog detail-one',
                                    icon: 'fa fa-list',
                                    url: 'wl_dingdan/jianyundan',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },
                                /* {
                                     name: 'ajax',
                                     text: __('分配车辆'),
                                     title: __('分配车辆'),
                                     classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                     icon: 'fa fa-magic',
                                     url: 'example/bootstraptable/detail',
                                     confirm: '确认发送',
                                     success: function (data, ret) {
                                         Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                         //如果需要阻止成功提示，则必须使用return false;
                                         //return false;
                                     },
                                     error: function (data, ret) {
                                         console.log(data, ret);
                                         Layer.alert(ret.msg);
                                         return false;
                                     }
                                 },
                                 {
                                     name: 'addtabs',
                                     text: __('新选项卡中打开'),
                                     title: __('新选项卡中打开'),
                                     classname: 'btn btn-xs btn-warning btn-addtabs',
                                     icon: 'fa fa-folder-o',
                                     url: 'example/bootstraptable/detail'
                                 }*/
                            ],
                            formatter: Table.api.formatter.operate},



                        {field: 'id', title: __('Id')},
                       // {field: 'user_id', title: __('User_id')},
                        {field: 'admin_id', title: __('Admin_id')},
                        {field: 'ordersn', title: __('Ordersn'), operate: 'LIKE'},
                        {field: 'huowu', title: __('Huowu'), operate: 'LIKE'},
                        {field: 'zhongliang', title: __('Zhongliang'), operate:'BETWEEN'},
                        {field: 'xiadanri', title: __('Xiadanri'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'start', title: __('Start'), operate: 'LIKE'},
                        {field: 'desti', title: __('Desti'), operate: 'LIKE'},
                        {field: 'kedanhao', title: __('Kedanhao'), operate: 'LIKE'},
                        {field: 'yewutype', title: __('Yewutype'), operate: false,
                            /*formatter: function (value, row, index, custom) {
                               if(value==0) return "询价";
                               if(value==1) return "委派";
                            }*/
                            },
                        {field: 'weituofang', title: __('Weituofang'), operate: 'LIKE'},
                        {field: 'fahuoren', title: __('Fahuoren'), operate: 'LIKE'},
                        {field: 'fahuoriqi', title: __('Fahuoriqi')},
                        {field: 'fahuotel', title: __('Fahuotel'), operate: 'LIKE'},
                        {field: 'fahuoaddr', title: __('Fahuoaddr'), operate: 'LIKE'},
                        {field: 'shouhuofang', title: __('Shouhuofang'), operate: 'LIKE'},
                        {field: 'shouhuoriqi', title: __('Shouhuoriqi')},
                        {field: 'shouhuoren', title: __('Shouhuoren'), operate: 'LIKE'},
                        {field: 'shouhuotel', title: __('Shouhuotel'), operate: 'LIKE'},
                        {field: 'shouhuoaddr', title: __('Shouhuoaddr'), operate: 'LIKE'},
                        {field: 'tihuofei', title: __('Tihuofei'), operate:'BETWEEN'},
                        {field: 'yunfei', title: __('Yunfei'), operate:'BETWEEN'},
                        {field: 'songhuofei', title: __('Songhuofei'), operate:'BETWEEN'},
                        {field: 'kongchefei', title: __('Kongchefei'), operate:'BETWEEN'},
                        {field: 'yachefei', title: __('Yachefei'), operate:'BETWEEN'},
                        {field: 'qitafei', title: __('Qitafei'), operate:'BETWEEN'},
                        {field: 'yufu', title: __('Yufu'), operate:'BETWEEN'},
                        {field: 'daofu', title: __('Daofu'), operate:'BETWEEN'},
                        {field: 'huidanfu', title: __('Huidanfu'), operate:'BETWEEN'},
                        {field: 'yuejie', title: __('Yuejie'), operate:'BETWEEN'},
                        {field: 'ddtype', title: __('Ddtype'), searchList: { "1":__('Assigned'),"2":__('Myself')}, formatter: Table.api.formatter.normal},

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(".btn-add").data("area", ["100%", "100%"]);

            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["100%", "100%"]);
                $(".detail-one").data("area", ["100%", "100%"]);
            });

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },

        jianyundan: function () {

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wl_dingdan/yundanlist/dingdanid/'+Config.dingdanid + location.search,
                    add_url: 'yundan/add/dingdanid/'+Config.dingdanid,
                    edit_url: 'yundan/edit',
                    del_url: 'yundan/del',
                    table: 'yundan',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        /*    {
                                field: 'buttons',
                                width: "120px",
                                title: __('按钮组'),
                                table: table,
                                events: Table.api.events.operate,
                                buttons: [
                                    /!*{
                                        name: 'detail',
                                        text: __('建运单'),
                                        title: __('建运单'),
                                        classname: 'btn btn-xs btn-primary btn-dialog',
                                        icon: 'fa fa-list',
                                        url: 'dingdan/jianyundan',
                                        callback: function (data) {
                                            Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                        },
                                        visible: function (row) {
                                            //返回true时按钮显示,返回false隐藏
                                            return true;
                                        }
                                    },*!/
                                    {
                                        name: 'ajax',
                                        text: __('派车'),
                                        title: __('派车'),
                                        classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                        icon: 'fa fa-magic',
                                        url: 'example/bootstraptable/detail',
                                        confirm: '确认发送',
                                        success: function (data, ret) {
                                            Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                            //如果需要阻止成功提示，则必须使用return false;
                                            //return false;
                                        },
                                        error: function (data, ret) {
                                            console.log(data, ret);
                                            Layer.alert(ret.msg);
                                            return false;
                                        }
                                    },
                                    /!*{
                                        name: 'addtabs',
                                        text: __('新选项卡中打开'),
                                        title: __('新选项卡中打开'),
                                        classname: 'btn btn-xs btn-warning btn-addtabs',
                                        icon: 'fa fa-folder-o',
                                        url: 'example/bootstraptable/detail'
                                    }*!/
                                ],
                                formatter: Table.api.formatter.buttons
                            },*/
                        {field: 'operate', title: __('派车'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},
                        {field: 'id', title: __('Id')},
                          {field: 'admin_id', title: __('Admin_id')},
                      //  {field: 'chengyun_id', title: __('Chengyun_id')},
                      //  {field: 'ordersn', title: __('Ordersn'), operate: 'LIKE'},
                      //  {field: 'xiadanri', title: __('Xiadanri'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'chepai', title: __('Chepai'), operate:false},
                        {field: 'checi', title: __('Checi'), operate:false},
                        {field: 'huowu', title: __('Huowu'), operate: 'LIKE'},
                        {field: 'zhongliang', title: __('Zhongliang'), operate:'BETWEEN'},
                        {field: 'start', title: __('Start'), operate: 'LIKE'},
                        {field: 'desti', title: __('Desti'), operate: 'LIKE'},

                    ]
                ]
            });

            $(".btn-add").data("area", ["100%", "100%"]);
            // 为表格绑定事件
            Table.api.bindevent(table);

            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                console.log(data);
                //这里我们手动设置底部的值
                $("#zhongliang").text(data.extend.zhongliang);
                $("#zongzhong").text(data.extend.zongzhong);
                //   $("#price").text(data.extend.price);
            });


            Controller.api.bindevent();
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});